#include "stdafx.h"
#include "test.h"
#include <stdlib.h>
DEFINE_GUID(InterfaceClassGuidConstant,0x058815B2, 0x9805, 0x47d3, 0xB7, 0xD5, 0xAB, 0xC4, 0x64, 0xD3, 0xCA, 0x06);

struct DeviceData {
	TCHAR  *HardwareId;
	TCHAR  *Path; // symbolic link
	TCHAR  *FriendlyName;
	DWORD  DeviceInstance;
} *DeviceList;


void fortest()
{
	// Example GUID definition. This GUID must match the GUID that was specified in the INF file 
	// that loaded winusb.sys. 

	
	GUID InterfaceClassGuid = InterfaceClassGuidConstant;

	// Define a handle to a device information set.
	// Retrieve a device information set.
	HDEVINFO hdevClassInfo = SetupDiGetClassDevs(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
	if(hdevClassInfo == INVALID_HANDLE_VALUE)
	{ 
		// ERROR
	}

	SP_DEVICE_INTERFACE_DATA  DeviceData;
	DeviceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);

	int nMemberIndex = 0;
	BOOL nStatus;

	for (nMemberIndex = 0; TRUE; nMemberIndex++) 
	{
		nStatus = SetupDiEnumDeviceInterfaces (hdevClassInfo, NULL,	(LPGUID)&InterfaceClassGuid, nMemberIndex,
			&DeviceData);
		if (nStatus != TRUE) break; //the nMemberIndex store the number of device interface.

		DWORD dwSize = 0;
		nStatus = SetupDiGetDeviceInterfaceDetail(hdevClassInfo, &DeviceData, NULL, 0, &dwSize, NULL);                  
		if (nStatus != TRUE) break;

		// Allocate memory for the device detail buffer. 

		PSP_INTERFACE_DEVICE_DETAIL_DATA pDeviceDetailData;
		pDeviceDetailData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(dwSize);
		if (pDeviceDetailData == NULL) 
		{ 
			// ERROR 
		}
		pDeviceDetailData->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
		SP_DEVINFO_DATA devInfo = { 0 }; //A pointer buffer to receive information about the device that supports the requested interface.
		devInfo.cbSize = sizeof(SP_DEVINFO_DATA);

		nStatus = SetupDiGetDeviceInterfaceDetail(hdevClassInfo, &DeviceData, pDeviceDetailData, dwSize, NULL, &devInfo);
		if (nStatus = FALSE)
		{ 
			// ERROR 
		}
		//TCHAR fname[256];   
		//if (!SetupDiGetDeviceRegistryProperty(hdevClassInfo, &devInfo,SPDRP_FRIENDLYNAME,NULL,(PBYTE) fname, sizeof(fname), NULL) 
			//&& !SetupDiGetDeviceRegistryProperty(hdevClassInfo, &devInfo,SPDRP_DEVICEDESC,NULL,(PBYTE) fname,sizeof(fname), NULL) ) 
		//
		//DWORD 	nStatus = SetupDiGetDeviceRegistryProperty(hdevClassInfo, &DevnfoData,
		//	SPDRP_FRIENDLYNAME, &dwRegType,
		//	NULL, 0, &dwRegSize);
		//if (nStatus == FALSE) { // ERROR }

		//	// Allocate buffer for the friendly name.
		//	pBuffer = (TCHAR *) malloc 
		//		(dwRegSize * sizeof(TCHAR));
	

		
		//then we can retrieve the hardware id.	
		
		if (NULL != pDeviceDetailData)
		{
			free(pDeviceDetailData);
			pDeviceDetailData = NULL;
		}

		
	} //end of for loop.
	
	SetupDiDestroyDeviceInfoList(hdevClassInfo);

}



